#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/11/1 17:20
# @Author  : 胡浩浩
# @File    : api_doc_api.py
# @IDE     : PyCharm
# @description :

from fastapi import FastAPI, Request
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from tortoise.contrib.fastapi import register_tortoise
from .api_doc_api import api_doc_router
from .count_data_api import count_data_router
from .group_api import group_router

from .test_sheet_api import test_sheet_router
from ..commons.comms import IPADDRESS

print(IPADDRESS)
app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="html")

# 注册Tortoise ORM
register_tortoise(
    app,
    # db_url=f'mysql://root:TH@cs2b_SH11@127.0.0.1:3306/api_system',
    db_url=f'mysql://root:Sa_7654321@127.0.0.1:3306/api_system',
    modules={
        "models":[
            "apps.models.api_doc_model",
            "apps.models.test_sheet_model",
            "apps.models.group_model",
            "apps.models.count_data_model",
        ]
    },
    generate_schemas=False,
    add_exception_handlers=False,
)
# 注册路由

app.include_router(api_doc_router)
app.include_router(test_sheet_router)
app.include_router(group_router)
app.include_router(count_data_router)


@app.get("/", response_class=HTMLResponse)
async def read_item(request: Request):
    context = {"request": request, "page_title": "欢迎页"}
    return templates.TemplateResponse("page.html", context=context)
